package icu.twtool.ui.component

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.LocalContentColor
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import icu.twtool.ui.style.MyColors
import icu.twtool.ui.style.MyShape

/**
 * @author Wen
 * @since 2022-11-06
 */
@Composable
@Suppress("FunctionName")
fun <T> Table(data: List<T>, headers: Array<TableProps<T>>, headerTextModifier: Modifier = Modifier) {
    Column(Modifier.padding(16.dp).clip(MyShape.RoundedCorner)) {
        TableHeader(headers, headerTextModifier)
        Surface(color = MyColors.TertiaryBackground, contentColor = MyColors.OnTertiaryBackground) {
            if (data.isEmpty()) EmptyBody()
            else TableBody(data, headers)
        }
    }
}

/**
 * 表头
 */
@Composable
@Suppress("FunctionName")
fun <T> TableHeader(tableProps: Array<TableProps<T>>, textModifier: Modifier) {
    Row(Modifier.fillMaxWidth().height(48.dp).background(MyColors.TableHeaderBackground)) {
        CompositionLocalProvider(LocalContentColor provides MyColors.TableHeaderContent) {
            for (item in tableProps) {
                Box(item.modifier(this), Alignment.Center) {
                    item.headerTemplate?.let { it(this, item.name) } ?: Text(item.name, textModifier)
                }
            }
        }
    }
}

/**
 * 表内容
 */
@Composable
@Suppress("FunctionName")
fun <T> TableBody(data: List<T>, tableProps: Array<TableProps<T>>) {
    LazyColumn {
        items(data.size, { data[it]!! }) {
            Row(Modifier.fillMaxWidth().height(38.dp)) {
                CompositionLocalProvider(LocalContentColor provides LocalContentColor.current.copy(0.7f)) {
                    for (prop in tableProps) {
                        Row (prop.modifier(this), horizontalArrangement = Arrangement.Center, verticalAlignment = Alignment.CenterVertically) {
                            prop.rowTemplate(this, data[it])
                        }
                    }
                }
            }
        }
    }
}

/**
 * 表格为空时
 */
@Composable
@Suppress("FunctionName")
fun EmptyBody() {
    Box(Modifier.fillMaxWidth().padding(32.dp), Alignment.Center) {
        val emptyIcon = painterResource("/icons/empty.svg")
        Image(emptyIcon, null)
    }
}

data class TableProps<T>(
    val name: String,
    val modifier: @Composable RowScope.() -> Modifier,
    val headerTemplate: (@Composable BoxScope.(String) -> Unit)? = null,
    val rowTemplate: (@Composable RowScope.(T) -> Unit)
)